package com.neouton.crm.approval.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.approval.entity.ApprovalVO;
import com.neouton.crm.approval.service.IApprovalService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("approval")
public class ApprovalController{

    @Resource
    private IApprovalService approvalService;

    /**
     * 查询费用记录
     *
     * @param approval
     * @return
     */
    @PostMapping("/queryApprovalByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                           @PathVariable(value = "pageSize") Integer pageSize,
                                           @RequestBody ApprovalVO approval) {
        IPage<ApprovalVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ApprovalVO> list = approvalService.queryApprovalByPage(page,approval);
        return ResponseAPI.successData(list);
    }


    /**
     * 查询审批日志
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @PostMapping("/queryApprovalRecordByType/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalRecordByType( @PathVariable(value = "pageNum") int pageNum,
                                                   @PathVariable(value = "pageSize") int pageSize,
                                                   @RequestBody ApprovalVO entity,
                                                   @RequestHeader("token") String token) {
        IPage<ApprovalVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ApprovalVO> result = approvalService.queryApprovalRecordByType(page ,entity,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 已办审批查询
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @PostMapping("/queryApprovalDoneRecordByType/{pageNum}/{pageSize}")
    public ResponseAPI queryApprovalDoneRecordByType( @PathVariable(value = "pageNum") int pageNum,
                                                  @PathVariable(value = "pageSize") int pageSize,
                                                  @RequestBody ApprovalVO entity) {
        PageUtil<ApprovalVO> result = approvalService.queryApprovalDoneRecordByType(pageNum,pageSize,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 查询审批实体
     *
     * @param approvalId
     * @return
     */
    @PostMapping("/queryApprovalById/{approvalId}")
    public ResponseAPI queryApprovalById(@PathVariable(value = "approvalId") Integer approvalId) {
        ApprovalVO approval = approvalService.queryApprovalByAppID(approvalId);
        return ResponseAPI.successData(approval);
    }


    /**
     * 查询审批日志
     * @param approvalId
     * @return
     */
    @PostMapping("/queryApprovalLogByAppID/{approvalId}")
    public ResponseAPI queryApprovalLogByAppID(@PathVariable(value = "approvalId") Integer approvalId){
        return ResponseAPI.successData(approvalService.queryApprovalLogByAppID(approvalId));
    }

    /**
     * 撤回申请
     * @param approval 工作流ID
     * @return
     */
    @PostMapping("/deleteInstance")
    public ResponseAPI deleteInstance(@RequestHeader("token") String token,@RequestBody ApprovalVO approval){
        return ResponseAPI.successData(approvalService.deleteInstance(approval,token));
    }

    /**
     * 获取审批日志
     * @param approvalId
     * @return
     */
    @PostMapping("/queryApprovalLog/{approvalId}")
    public ResponseAPI queryApprovalLog(@PathVariable("approvalId") Integer approvalId){
        return ResponseAPI.successData( approvalService.queryApprovalLog(approvalId));
    }

    /**
     * 发起撤回审批
     * @param approval
     * @return
     */
    @PostMapping("/saveWithdraw")
    public ResponseAPI saveWithdraw(@RequestHeader("token") String token,@RequestBody ApprovalVO approval){
        return ResponseAPI.successData( approvalService.saveWithdraw(approval,token));
    }

    /**
     * 通过撤回审批
     * @param approval
     * @return
     */
    @PostMapping("/approveWithdraw")
    public ResponseAPI approveWithdraw(@RequestHeader("token") String token,@RequestBody ApprovalVO approval){
        return ResponseAPI.successData( approvalService.approveWithdraw(approval,token));
    }

    /**
     * 驳回撤回审批
     * @param approval
     * @return
     */
    @PostMapping("/rejectionWithdraw")
    public ResponseAPI rejectionWithdraw(@RequestHeader("token") String token,@RequestBody ApprovalVO approval){
        return ResponseAPI.successData( approvalService.rejectionWithdraw(approval,token));
    }

    /**
     * 分页查询待办任务
     * @param page
     * @param approval
     * @return
     */
    @PostMapping("/queryWithdrawalApprovalByPage/{pageNum}/{pageSize}")
    public ResponseAPI queryWithdrawalApprovalByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                     @PathVariable(value = "pageSize") Integer pageSize,
                                                     @RequestBody ApprovalVO approval){
        IPage<ApprovalVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ApprovalVO> list = approvalService.queryWithdrawalApprovalByPage(page,approval);
        return ResponseAPI.successData(list);
    }

    /**
     * 已办审批查询
     * @param pageNum
     * @param pageSize
     * @param approval
     * @return
     */
    @PostMapping("/queryWithdrawalApprovalDoneRecordByType/{pageNum}/{pageSize}")
    public ResponseAPI queryWithdrawalApprovalDoneRecordByType( @PathVariable(value = "pageNum") int pageNum,
                                                                @PathVariable(value = "pageSize") int pageSize,
                                                                @RequestBody ApprovalVO entity) {
        PageUtil<ApprovalVO> result = approvalService.queryWithdrawalApprovalDoneRecordByType(pageNum,pageSize,entity);
        return ResponseAPI.successData(result);
    }

    /**
     * 查询审批记录，更具审批类型
     * @param page 分页
     * @param approval 审批实体类
     * @return
     */
    @PostMapping("/queryWithdrawalApprovalRecordByType/{pageNum}/{pageSize}")
    public ResponseAPI queryWithdrawalApprovalRecordByType( @PathVariable(value = "pageNum") int pageNum,
                                                            @PathVariable(value = "pageSize") int pageSize,
                                                            @RequestBody ApprovalVO entity,
                                                            @RequestHeader("token") String token) {
        IPage<ApprovalVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ApprovalVO> result = approvalService.queryWithdrawalApprovalRecordByType(page ,entity,token);
        return ResponseAPI.successData(result);

    }

    /**
     * 查询审批记录，更具审批类型
     * @param approvalCode 审批Code
     * @return
     */
    @PostMapping("/queryApprovalStepsByApprovalCode/{approvalCode}")
    public ResponseAPI queryApprovalStepsByApprovalCode( @PathVariable(value = "approvalCode") String approvalCode) {
        List<String> result = approvalService.queryApprovalStepsByApprovalCode(approvalCode);
        return ResponseAPI.successData(result);

    }
}
